----------------------------------------------------------------
-- Local Functions (placed here to avoid dependency issues)
----------------------------------------------------------------

----------------------------------------------------------------
-- Global Variables
----------------------------------------------------------------

EA_Window_Rename = {}

local containerWindowName = "EA_Window_Rename_Container"
local windowName = "EA_Window_Rename"
EA_Window_Rename.newNameText = L""

function EA_Window_Rename.Initialize()
    -- Set the text of all the buttons/labels
    LabelSetText( windowName.."KeyInputLabel", GetPregameString( StringTables.Pregame.LABEL_RENAME_EDITBOX ) )
    LabelSetText( windowName.."TitleBarText", GetPregameString( StringTables.Pregame.LABEL_RENAME_WINDOW ) )
    LabelSetText( windowName.."MessageText", GetPregameString( StringTables.Pregame.LABEL_RENAME_PLAYER ) )
    ButtonSetText( windowName.."Accept", GetPregameString( StringTables.Pregame.LABEL_RENAME_ACCEPT) )
    
    WindowRegisterEventHandler( windowName, SystemData.Events.CHARACTER_PREGAME_NAMING_CONFLICT_POP_UP_WINDOW, "EA_Window_Rename.PopUp" )
    WindowRegisterEventHandler( windowName, SystemData.Events.CHARACTER_PREGAME_NAMING_CONFLICT_RESPONSE, "EA_Window_Rename.Response" )
end

function EA_Window_Rename.Hide()
    WindowSetShowing( containerWindowName, false )
end

function EA_Window_Rename.Show()
    WindowSetShowing( containerWindowName, true )
    WindowAssignFocus(windowName.."TextInput", true)
end

function EA_Window_Rename.LockInput()
    ButtonSetDisabledFlag(windowName.."Accept", true)
end

function EA_Window_Rename.UnlockInput()
    ButtonSetDisabledFlag(windowName.."Accept", false)
end

function EA_Window_Rename.OnAcceptButton()
    local newName = TextEditBoxGetText( windowName.."TextInput" )
    if( not ButtonGetDisabledFlag(windowName.."Accept") and newName and newName ~= L"" )
    then
        LabelSetText( windowName.."MessageText", GetPregameString( StringTables.Pregame.LABEL_RENAME_VALIDATING_NEW_NAME) )
        EA_Window_Rename.LockInput()
        NamingConflictRequestNewName( EA_Window_Rename.newNameText )
    elseif( newName == nil or newName == L"" )
    then
        LabelSetText( windowName.."MessageText", GetPregameString( StringTables.Pregame.LABEL_RENAME_VALIDATING_ERROR ) )
    end
end

function EA_Window_Rename.PopUp()
    LabelSetText( windowName.."MessageText", GetPregameString( StringTables.Pregame.LABEL_RENAME_PLAYER ) )
    TextEditBoxSetText( windowName.."TextInput", L"" )
    EA_Window_Rename.newNameText = L""
    EA_Window_Rename.UnlockInput()
    EA_Window_Rename.Show()
end

function EA_Window_Rename.Response( success )
    if( success )
    then
        EA_Window_Rename.Hide()
    else
        LabelSetText( windowName.."MessageText", GetPregameString( StringTables.Pregame.LABEL_RENAME_VALIDATING_ERROR ) )
    end
    
    EA_Window_Rename.UnlockInput()
end

function EA_Window_Rename.OnTextChanged()
    local text = TextEditBoxGetText( windowName.."TextInput" )
    if ( text and text ~= L"" )
    then
		-- If the name has changed, ensure only the first letter is capitalized.
		if( text ~= EA_Window_Rename.newNameText )
		then
			local firstChar = wstring.upper( wstring.sub( text, 1, 1 ) )
			local remaining = L""
			if( wstring.len(text) > 1 )
			then
				remaining = wstring.lower( wstring.sub( text, 2, 14 ) )
			end
			text = firstChar..remaining
			TextEditBoxSetText(windowName.."TextInput", text ) 
		end
		
		EA_Window_Rename.newNameText = text
    else
        EA_Window_Rename.newNameText = L""
	end
end
